package com.uenpay.oem.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.oem.R;
import com.uenpay.oem.util.bankcard.BankCardUtils;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.util.widget.sliderecyclerview.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by DELL on 2017/6/19.
 */

public class BankCardAdapter extends SwipeMenuAdapter<BankCardAdapter.ViewHolder> {
    private List<BankCardInfo> data;
    private Context context ;

    public BankCardAdapter(Context context, List<BankCardInfo> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bankcard, null);
        return view;
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final BankCardInfo cardInfo = data.get(position);
        String cardNum = cardInfo.getCardNo();
        holder.bankName.setText(cardInfo.getBankName());
        holder.cardNum.setText(BankCardUtils.getSubBankNum(cardNum, BankCardUtils.REMAIN));
        String cardtype = cardInfo.getCardType();
        if("1".equals(cardtype)){
            holder.cardType.setText("储蓄卡");
        } else {
            holder.cardType.setText("信用卡");
        }
        if(position ==0){
            cardInfo.isDefault = true;
        } else {
            cardInfo.isDefault = false;
        }
        if (cardInfo.isDefault){
            holder.defaultCard.setVisibility(View.VISIBLE);
        } else {
            holder.defaultCard.setVisibility(View.INVISIBLE);
        }
        holder.icon.setVisibility(View.VISIBLE);
        holder.tvHide.setTextColor(Color.WHITE);
        holder.bankName.setTextColor(Color.WHITE);
        holder.cardType.setTextColor(Color.WHITE);
        holder.cardNum.setTextColor(Color.WHITE);
        switch (cardInfo.getBankName()) {
            case "招商银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_zhaoshang);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_red);
                break;
            case "中国银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_china);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_red);
                break;
            case "中信银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_zhongxin);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_red);
                break;
            case "浦发银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_pufa);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_blue);
                break;
            case "建设银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_jianshe);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_blue);
                break;
            case "工商银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_gongshang);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_red);
                break;
            case "交通银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_jiaotong);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_blue);
                break;
            case "民生银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_minsheng);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_blue);
                break;
            case "农业银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_nongye);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_green);
                break;
            case "邮政储蓄银行":
                holder.icon.setBackgroundResource(R.drawable.icon_bank_youzheng);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_green);
                break;
            default:
                holder.icon.setVisibility(View.INVISIBLE);
                holder.bankName.setTextColor(0xff3c3c3c);
                holder.cardType.setTextColor(0xff3c3c3c);
                holder.cardNum.setTextColor(0xff3c3c3c);
                holder.tvHide.setTextColor(0xff3c3c3c);
                holder.imBg.setBackgroundResource(R.drawable.bg_bank_white);
        }

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView bankName;
        TextView cardType;
        TextView tvHide;
        TextView cardNum;
        ImageView imBg;
        ImageView icon;
        ImageView defaultCard;
        public ViewHolder(View itemView) {
            super(itemView);
            bankName = (TextView) itemView.findViewById(R.id.tv_bank_name);
            cardType = (TextView) itemView.findViewById(R.id.tv_card_type);
            cardNum = (TextView) itemView.findViewById(R.id.tv_card_num);
            imBg = (ImageView) itemView.findViewById(R.id.iv_card_bg);
            icon = (ImageView) itemView.findViewById(R.id.iv_bank_icon);
            defaultCard = (ImageView) itemView.findViewById(R.id.iv_default_card);
            tvHide = (TextView) itemView.findViewById(R.id.tv_hide);
        }
    }
}

